﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoxBotController : MonoBehaviour 
{

	private Rigidbody2D rb2D;
	private Animator anim;

	private enum BoxBotDirection : int{North, East, South, West, NorthEast, NorthWest, SouthEast, SouthWest};
	private BoxBotDirection boxBotDirection;
	private Vector2 moveDir;
	public int boxBotFloor;

	public float boxBotSpeed;

	void Start () 
	{
		rb2D = GetComponent<Rigidbody2D>();
		anim = GetComponent<Animator>();

		//Set a default startup direction
		boxBotDirection = BoxBotDirection.South;

		//Testing!
		InvokeRepeating ("RandomRotBoxBot",1.0f,5.0f);
		boxBotSpeed = UnityEngine.Random.Range(0.2f,1f);
		boxBotFloor = 0;
	}


	void FixedUpdate()
	{
		// Move Blobo
		rb2D.MovePosition(rb2D.position + moveDir * (boxBotSpeed * Time.fixedDeltaTime));

	}

	// Update is called once per frame
	void Update () 
	{
		switch (boxBotDirection)
		{
		case BoxBotDirection.North:
			moveDir = new Vector2(0,1);
			anim.Play("BoxBot_Move_N");
			break;
		case BoxBotDirection.NorthEast:
			moveDir = new Vector2(1,1);
			anim.Play("BoxBot_Move_NE");
			break;
		case BoxBotDirection.NorthWest:
			moveDir = new Vector2(-1,1);
			anim.Play("BoxBot_Move_NW");
			break;
		case BoxBotDirection.South:
			moveDir = new Vector2(0,-1);
			anim.Play("BoxBot_Move_S");
			break;
		case BoxBotDirection.SouthEast:
			moveDir = new Vector2(1,-1);
			anim.Play("BoxBot_Move_SE");
			break;
		case BoxBotDirection.SouthWest:
			moveDir = new Vector2(-1,-1);
			anim.Play("BoxBot_Move_SW");
			break;
		case BoxBotDirection.East:
			moveDir = new Vector2(1,0);
			anim.Play("BoxBot_Move_E");
			break;
		case BoxBotDirection.West:
			moveDir = new Vector2(-1,0);
			anim.Play("BoxBot_Move_W");
			break;
		default:
			break;
		}
	}

	protected void OnCollisionEnter2D(Collision2D collider)
	{

	}

	protected void OnTriggerEnter2D(Collider2D collider)
	{
		RandomRotBoxBot();
	}

	//Function to randomly select a direction for Blobo
	protected void RandomRotBoxBot()
	{
		boxBotDirection = (BoxBotDirection)(UnityEngine.Random.Range(0, Enum.GetNames(typeof(BoxBotDirection)).Length));
	}
}
